//
//  LS_SearchView.m
//  yiYuanDuoBao
//
//  Created by 罗李苏 on 16/11/29.
//  Copyright © 2016年 罗李苏. All rights reserved.
//

#import "LS_SearchView.h"

@interface LS_SearchView ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *searchField;



@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIImageView *leftView;
@property (nonatomic,strong) UIButton *deleteBtn;

@end
@implementation LS_SearchView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setUpUi];
        [self initAllViews];
    }
    return self;
}

- (void)setUpUi{

    UITextField *searchField = [[UITextField alloc] init];
    //设置键盘样式
    searchField.keyboardType = UIKeyboardTypeDefault;
    //设置键盘外观
    searchField.keyboardAppearance = UIKeyboardAppearanceAlert;
//    searchField.inputView 可以自定义弹出个视图,设置的frame时，只有高度有用，其他x和y和宽都是无效的，宽是默认的整个键盘宽度
    //设置占位文字
    searchField.placeholder = @"搜索商品";
    //设置清除
    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
//    [btn setBackgroundImage:[UIImage imageNamed:@"搜索页_搜索_小_icon"] forState:UIControlStateNormal];
    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索页_搜索_小_icon"]];
    view.frame = CGRectMake(12, 4, 24, 24);
    searchField.leftView = view;
    searchField.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"搜索页_清除文字_icon"] forState:UIControlStateNormal];
    [btn1 addTarget:searchField action:@selector(cleanAll:) forControlEvents:UIControlEventTouchUpInside];
    searchField.rightView = btn1;
    searchField.rightViewMode = UITextFieldViewModeAlways;
    _searchField = searchField;
    [self addSubview:_searchField];
    //添加约束
    [_searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(6);
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-64);
        
    }];
    
    //取消按钮的设置
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 16)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithString:@"F84A0D"] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:1];
    _cancelBtn = cancelBtn;
    [self addSubview:_cancelBtn];
     [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_searchField.mas_right).offset(16);
         make.centerY.mas_equalTo(_searchField);
    }];
}

- (void)cleanAll:(UIButton *)sender{

    
    _searchField.text = @"";
    
}

-(void)initAllViews
{
//    self.layer.cornerRadius = self.frame.size.height / 2;
//    self.layer.masksToBounds = YES;
    
    if (_leftView == nil) {
//        _leftView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.frame.size.height - 17) / 2.0f, 17, 17)];
        _leftView = [[UIImageView alloc] initWithFrame:CGRectMake(12,4, 24, 24)];
        _leftView.image = [UIImage imageNamed:@"搜索页_搜索_小_icon"];
//        [self addSubview:_leftView];
    }
    
    
    if (_deleteBtn == nil) {
//        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _deleteBtn.frame = CGRectMake(self.frame.size.width - self.frame.size.height - 4, 2, self.frame.size.height - 4, self.frame.size.height - 4);
        
        _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        [_deleteBtn setImage:[UIImage imageNamed:@"搜索页_清除文字_icon"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:_deleteBtn];
    }
    if (_textField == nil) {
//        _textField = [[UITextField alloc] initWithFrame:CGRectMake(_leftView.frame.size.width +     _leftView.frame.origin.x + 10, 2, self.frame.size.width - (_leftView.frame.size.width + _leftView.frame.origin.x + 10) - (self.frame.size.height - 4), self.frame.size.height - 4)];
        _textField = [[UITextField alloc] init];
        _textField.backgroundColor = [UIColor yellowColor];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(textFieldValueChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];
        //设置边框样式
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.leftView = _leftView;
        _textField.leftViewMode = UITextFieldViewModeAlways;

        _textField.rightView = _deleteBtn;
        _textField.rightViewMode = UITextFieldViewModeAlways;
        
        //设置键盘样式
        _textField.keyboardType = UIKeyboardTypeDefault;
        //设置键盘外观
        _textField.keyboardAppearance = UIKeyboardAppearanceAlert;
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(12);
            make.left.equalTo(self).offset(16);
            make.right.equalTo(self).offset(-64);
        }];

    }

    
    //取消按钮的设置
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 16)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithString:@"F84A0D"] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight:1];
    _cancelBtn = cancelBtn;
    [self addSubview:_cancelBtn];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textField.mas_right).offset(16);
        make.centerY.mas_equalTo(_textField);
    }];

}
-(void)setIsBecomeFirstResponder:(BOOL)isBecomeFirstResponder
{
    _isBecomeFirstResponder = isBecomeFirstResponder;
    if (_isBecomeFirstResponder) {
        [_textField becomeFirstResponder];
    }
}

-(void)setSearchBarColor:(UIColor *)searchBarColor
{
    _searchBarColor = searchBarColor;
//    self.backgroundColor = _searchBarColor;
    _textField.backgroundColor = _searchBarColor;
    _leftView.backgroundColor  = _searchBarColor;
//    _deleteBtn.backgroundColor = _searchBarColor;
}
-(void)setSearchBarFont:(UIFont *)searchBarFont
{
    _searchBarFont = searchBarFont;
    _textField.font = _searchBarFont;
}

-(void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    _textField.textColor = textColor;
}

-(void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = placeHolder;
    _textField.placeholder = _placeHolder;
}

-(void)setPlaceHoldersColor:(UIColor *)placeHoldersColor
{
    _placeHoldersColor = placeHoldersColor;
    [_textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
}

-(void)setPlaceHolderFont:(UIFont *)placeHolderFont
{
    _placeHolderFont = placeHolderFont;
    [_textField setValue:_placeHolderFont forKeyPath:@"_placeholderLabel.font"];
}

-(void)setText:(NSString *)text
{
    _text = text;
    _textField.text = _text;
}

-(void)setDeleteImage:(UIImage *)deleteImage
{
    _deleteImage = deleteImage;
    [_deleteBtn setImage:_deleteImage forState:UIControlStateNormal];
}

#pragma mark - - deleteClick
-(void)deleteClick:(UIButton *)delete
{
    /**
     *  删除
     */
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarCancelButtonClicked:)]) {
        [self.delegate searchBarCancelButtonClicked:self];
    }
}

#pragma mark - - textFieldValueChange
-(void)textFieldValueChange:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBar:textDidChange:)]) {
        [self.delegate searchBar:self textDidChange:textField.text];
    }
}

#pragma mark - - UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        [self.delegate searchBarShouldBeginEditing:self];
    }
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidBeginEditing:)]) {
        [self.delegate searchBarTextDidBeginEditing:self];
    }
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidEndEditing:)]) {
        [self.delegate searchBarShouldEndEditing:self];
    }
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidEndEditing:)]) {
        [self.delegate searchBarTextDidEndEditing:self];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_delegate && [_delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
        [self.delegate searchBarSearchButtonClicked:self];
    }
    return YES;
}

/*
 ＊＊＊＊＊＊ ［在ViewController_ViewDidLoad中调用：］ ＊＊＊＊＊＊＊
 
 LS_SearchView *searchBar = [[LS_SearchView alloc] initWithFrame:CGRectMake(20, 80,     self.view.frame.size.width - 40, 40)];
 searchBar.placeHolder = @"搜索";
 searchBar.searchBarColor = [UIColor colorWithRed:205 / 255.0  green:235 / 255.0 blue:206 / 255.0 alpha:1.0f];
 searchBar.textColor = [UIColor whiteColor];
 searchBar.searchBarFont = [UIFont systemFontOfSize:15.0];
 searchBar.placeHoldersColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0f];
 searchBar.placeHolderFont = [UIFont systemFontOfSize:15.0f];
 searchBar.delegate = self;
 [self.view addSubview:searchBar];
 */
@end
